package com.zhy.skinchangenow.home

import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.water.skin.SkinManager
import com.water.skin.callback.ISkinChangingCallback
import com.zhy.skinchangenow.R
import java.io.File
import java.lang.Exception

class MenuLeftFragment : Fragment(), View.OnClickListener {

    private val mSkinPkgPath =
        Environment.getExternalStorageDirectory().toString() + File.separator + "skin_plugin.apk"
    private var mInnerChange01: View? = null
    private var mInnerChange02: View? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_left_menu, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mInnerChange01 = view.findViewById(R.id.id_rl_innerchange01)
        mInnerChange01?.setOnClickListener(this)
        mInnerChange02 = view.findViewById(R.id.id_rl_innerchange02)
        mInnerChange02?.setOnClickListener(this)
        view.findViewById<View>(R.id.id_restore)
            .setOnClickListener { SkinManager.getInstance().removeAnySkin() }
        view.findViewById<View>(R.id.id_changeskin).setOnClickListener {
            SkinManager.getInstance()
                .changeSkin(mSkinPkgPath, "com.imooc.skin_plugin", object : ISkinChangingCallback {
                    override fun onStart() {}
                    override fun onError(e: Exception) {
                        Toast.makeText(activity, "换肤失败", Toast.LENGTH_SHORT).show()
                    }

                    override fun onComplete() {
                        Toast.makeText(activity, "换肤成功", Toast.LENGTH_SHORT).show()
                    }
                })
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View) {
        when (v.id) {
            R.id.id_rl_innerchange01 -> SkinManager.getInstance().changeSkin("red")
            R.id.id_rl_innerchange02 -> SkinManager.getInstance().changeSkin("green")
        }
    }
}